Page({
    data: {
        username: '',
        password: '',
        redirect: '', 
        isLoggedIn: false,
        address: ''
    },
  
    onLoad(options) {
        if (options.redirect) {
            this.setData({ redirect: decodeURIComponent(options.redirect) });
            console.log(this.data.redirect);
        }
    },
  
    onUsernameInput(e) {
        this.setData({
            username: e.detail.value
        });
    },
  
    onPasswordInput(e) {
        this.setData({
            password: e.detail.value
        });
    },
  
    login() {
        const { username, password, redirect } = this.data;
        wx.request({
            url: 'http://localhost:8080/pubUser/login',
            method: 'POST',
            header: {
                'Content-Type': 'application/x-www-form-urlencoded'
            },
            data: {
                username,
                password,
                redirect,
                address: this.data.address
            },
            success: (res) => {
                if (res.data.code === 1) {
                    wx.showToast({
                        title: '登录成功',
                        icon: 'success'
                    });
                    wx.setStorageSync('token', '模拟token');
                    const app = getApp();
                    app.globalData.userId = res.data.data.id;
  
                    wx.request({
                        url: 'http://localhost:8080/pubUser/getUserById',
                        method: 'GET',
                        data: {
                            id: app.globalData.userId
                        },
                        success: (userRes) => {
                            if (userRes.data.code === 1) {
                                const userData = userRes.data.data;
                                app.globalData.userInfo = {
                                    id: userData.id,
                                    address: userData.address,
                                    point: userData.point,
                                    username:userData.username
                                };
                            } else {
                                wx.showToast({
                                    title: userRes.data.msg || '获取用户信息失败',
                                    icon: 'none'
                                });
                            }
                        },
                        fail: (err) => {
                            wx.showToast({
                                title: '网络请求失败',
                                icon: 'none'
                            });
                        },
                        complete: () => {
                            if (redirect) {
                                wx.redirectTo({ url: redirect });
                            } else {
                                wx.navigateBack();
                            }
                        }
                    });
                } else {
                    wx.showToast({
                        title: res.data.msg,
                        icon: 'none'
                    });
                }
            },
            fail: (err) => {
                wx.showToast({
                    title: '网络请求失败',
                    icon: 'none'
                });
            }
        });
    },
  
    register() {
        console.log("923");
        wx.navigateTo({
            url: '/pages/register_hy/register_hy',
        });
    },
  
    forgotPassword() {
        wx.showToast({ title: '请联系管理员重置密码', icon: 'none' });
    },
  
    onClickLeft() {
        const pages = getCurrentPages();
        if (pages.length > 1) {
            wx.navigateBack({
                delta: 1,
                success: () => {
                    console.log('成功返回上一页');
                },
                fail: (err) => {
                    console.error('返回上一页失败:', err);
                }
            });
        } else {
            console.log('当前页面栈中只有一个页面，无法返回上一页');
        }
    }
  });